<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Pay extends Controller
{

	/**
	 * 微信支付服务端验证异步通知信息
	 *$data['total_fee'] 返回的在线支付的金额
	 **/
	public function wechatApiBack(){
		//获取通知的数据
		//$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
		 $xml = file_get_contents('php://input');
		// file_put_contents('a.txt', $xml);
		$data = array();
		if( empty($xml) ){
			return false;
		}

		$data = $this->xml_to_data( $xml );


		if(!empty($data['return_code'])){
			if( $data['return_code'] == 'FAIL' ){
				return false;
			}
			//****充值后操作。

			$out_trade_no = $data['out_trade_no'];
			//查询充值金额设置

			$order_status = Db::name('order_common')->where(['order_sn'=>$out_trade_no])->value('order_status');

			if($order_status == 0){
				$payData['pay_time'] = time();
				Db::name('pay_log')->where(['out_trade_no'=>$out_trade_no])->update($payData);
				Db::name('order_common')->where(['order_sn'=>$out_trade_no])->update(['order_status'=>1,'pay_time'=>$payData['pay_time']]);
			}


		}
	}
    public function ttApiBack(){

        //获取通知的数据
        //$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
        $xml = file_get_contents('php://input');
        //file_put_contents('a.txt', $xml);

        $jsonData=$this->jsonDecode($xml);

        if(!empty($jsonData)){
            if( $jsonData['status'] != 'SUCCESS' ){
                return false;
            }
            //****充值后操作。

            $out_trade_no = $jsonData['cp_orderno'];
            //查询充值金额设置

            $is_pay = Db::table('mp_pay_log')->where(['out_trade_no'=>$out_trade_no])->value('is_pay');

            if($is_pay != 1){
                $payData['pay_time'] = date('Y-m-d H:i:s',time());
                $payData['is_pay'] = 1;
                Db::table('mp_pay_log')->where(['out_trade_no'=>$out_trade_no])->update($payData);

                $uid = Db::table('mp_pay_log')->where(['out_trade_no'=>$out_trade_no])->value('uid');

                Db::name('user')->where(['uid'=>$uid])->update(['is_vip'=>1,'buy_time'=>$payData['pay_time']]);
            }

        }

        return json(['err_no'=>0,'err_tips'=>'success']);
    }
	/**
	 * 将xml转为array
	 * @param string $xml
	 * @return array|boolean
	 */
	public function xml_to_data($xml){
		if(!$xml){
			return false;
		}
		//将XML转为array
		//禁止引用外部xml实体
		libxml_disable_entity_loader(true);
		$data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
		return $data;
	}

    public function jsonDecode($str){
        //file_put_contents('b.txt', $str['msg']);
        $str=str_replace('\\\"','',$str);
        $str=str_replace('\"',"'",$str);
        $str=str_replace("\\",'',$str);
        //file_put_contents('c.txt', $str);
        $jsonDecode=json_decode($str,true);
        $msg=$jsonDecode['msg'];
        //file_put_contents('d.txt', json_encode($msg));
        $str=str_replace("'",'"',$msg);
        $jsonData=json_decode($str,true);
        //file_put_contents('e.txt', json_encode($jsonData));
        return $jsonData;
    }

    function tt_sign($map) {
        $rList = array();
        foreach($map as $k =>$v) {
            if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
                continue;
            $value = trim(strval($v));
            $len = strlen($value);
            if ($len > 1 && substr($value, 0,1)=="\"" && substr($value,$len, $len-1)=="\"")
                $value = substr($value,1, $len-1);
            $value = trim($value);
            if ($value == "" || $value == "null")
                continue;
            array_push($rList, $value);
        }
        array_push($rList, "KZZE4rmTaLGBS1KctVxtHDbwfB4GSr3JFEN6qKIq");
        sort($rList, 2);
        return md5(implode('&', $rList));
    }

}